package com.yizhiliulianta.tool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolUtils {

    private ThreadPoolUtils() {
    }

    public static void shutDownPool(ExecutorService pool,
                                    int shutDownTimeout,
                                    int shutDownNowTimeout,
                                    TimeUnit timeUnit) {

        pool.shutdown();
        try {
            if (!pool.awaitTermination(shutDownTimeout, timeUnit)) {
                //等待超时时间后，仍没有处理完线程任务
                pool.shutdownNow();
                if (!pool.awaitTermination(shutDownNowTimeout, timeUnit)) {
                    log.error("ThreadPoolUtils.shutDownPool.error");
                }
            }
        } catch (InterruptedException e) {
            log.error("ThreadPoolUtils.shutDownPool.interrupt.error:{}", e.getMessage(), e);
            pool.shutdownNow();
            Thread.currentThread().interrupt();
        }

    }


}
